package com.zkj.reflect;

import java.lang.reflect.Constructor;

import org.junit.Test;

public class ReflectDemo1 {

	@Test
	public void test() throws Exception {
		Class clazz = Class.forName("com.zkj.reflect.Person");
		Constructor p = clazz.getConstructor(null);// 只能得到公有的构造方法
		Person s = (Person) p.newInstance(null);
		System.out.println(s.name);
	}

	@Test
	public void test2() throws Exception {
		Class clazz = Class.forName("com.zkj.reflect.Person");
		Constructor p = clazz.getDeclaredConstructor(int.class);// 得到私有构造方法
		p.setAccessible(true);// 强制执行
		p.newInstance(20);

	}

	@Test
	public void test3() throws Exception {
		System.out.println("----------------------------------------------");
		Class clazz = Class.forName("com.zkj.reflect.Person");
		Constructor[] p = clazz.getDeclaredConstructors();// 得到私有构造方法
		System.out.println(p.length);
	}
}
